using System;
using System.Collections.Generic;
using System.Text;
using System.Data;           //CommandType
using System.Data.SqlClient; //SqlConnection, SqlCommand, SqlDataReader
using Etna.Exportaciones.En;


namespace Etna.Exportaciones.Ad
{
   public class AdGuiaAerea
   {
      //public AdGuiaAerea(SqlConnection con)
      //{
      //   conexion = con;
      //}

       public EnGuiaAerea Leer(int pidGuiaAerea, SqlConnection conexion)
      {
         EnGuiaAerea oEnGuiaAerea = null;
         SqlCommand cmd = new SqlCommand("sExportaciones_GuiaAereaLeer", conexion);
         cmd.CommandType = CommandType.StoredProcedure;

         SqlParameter Parametro1 = cmd.Parameters.Add("@piidGuiaAerea", SqlDbType.Int);
         Parametro1.Direction = ParameterDirection.Input;
         Parametro1.Value = pidGuiaAerea;

         SqlDataReader drd = cmd.ExecuteReader(CommandBehavior.SingleResult);
         if (drd != null)
         {

            if (drd.Read())
            {
               oEnGuiaAerea = new EnGuiaAerea();
               oEnGuiaAerea.idGuiaAerea = drd.GetInt32(0);
               if (!drd.IsDBNull(1))
               {
                  oEnGuiaAerea.idFolioDespacho = drd.GetInt32(1);
               }
               if (!drd.IsDBNull(2))
               {
                  oEnGuiaAerea.sfechaEmbarque = drd.GetString(2);
               }
               if (!drd.IsDBNull(3))
               {
                  oEnGuiaAerea.shoraEmbarque = drd.GetString(3);
               }
               if (!drd.IsDBNull(4))
               {
                  oEnGuiaAerea.sflagEntregado = drd.GetString(4);
               }
               if (!drd.IsDBNull(5))
               {
                  oEnGuiaAerea.nfactorAjuste = drd.GetDecimal(5);
               }
               if (!drd.IsDBNull(6))
               {
                  oEnGuiaAerea.sconocimientoEmbarque = drd.GetString(6);
               }
               if (!drd.IsDBNull(7))
               {
                  oEnGuiaAerea.posicion = drd.GetString(7);
               }
               if (!drd.IsDBNull(8))
               {
                  oEnGuiaAerea.terminos = drd.GetString(8);
               }
               if (!drd.IsDBNull(9))
               {
                  oEnGuiaAerea.sobservacion = drd.GetString(9);
               }
            }
            drd.Close();
         }

         return oEnGuiaAerea;
      }

       public bool Insertar(EnGuiaAerea poEnGuiaAerea, SqlConnection conexion)
      {
         bool bExito = false;
         SqlCommand cmd = new SqlCommand("sExportaciones_GuiaAereaInsertar", conexion);
         cmd.CommandType = CommandType.StoredProcedure;

         SqlParameter Parametro1 = cmd.Parameters.Add("@piidGuiaAerea", SqlDbType.Int);
         Parametro1.Direction = ParameterDirection.Input;
         Parametro1.Value = poEnGuiaAerea.idGuiaAerea;

         SqlParameter Parametro2 = cmd.Parameters.Add("@piidFolioDespacho", SqlDbType.Int);
         Parametro2.Direction = ParameterDirection.Input;
         Parametro2.Value = poEnGuiaAerea.idFolioDespacho;

         SqlParameter Parametro3 = cmd.Parameters.Add("@pifechaEmbarque", SqlDbType.VarChar);
         Parametro3.Direction = ParameterDirection.Input;
         Parametro3.Value = poEnGuiaAerea.sfechaEmbarque;

         SqlParameter Parametro4 = cmd.Parameters.Add("@pihoraEmbarque", SqlDbType.VarChar);
         Parametro4.Direction = ParameterDirection.Input;
         Parametro4.Value = poEnGuiaAerea.shoraEmbarque;

         SqlParameter Parametro5 = cmd.Parameters.Add("@piflagEntregado", SqlDbType.VarChar);
         Parametro5.Direction = ParameterDirection.Input;
         Parametro5.Value = poEnGuiaAerea.sflagEntregado;

         SqlParameter Parametro6 = cmd.Parameters.Add("@pifactorAjuste", SqlDbType.Decimal);
         Parametro6.Direction = ParameterDirection.Input;
         Parametro6.Value = poEnGuiaAerea.nfactorAjuste;

         SqlParameter Parametro7 = cmd.Parameters.Add("@piconocimientoEmbarque", SqlDbType.VarChar);
         Parametro7.Direction = ParameterDirection.Input;
         Parametro7.Value = poEnGuiaAerea.sconocimientoEmbarque;

         SqlParameter Parametro8 = cmd.Parameters.Add("@piposicion", SqlDbType.NChar);
         Parametro8.Direction = ParameterDirection.Input;
         Parametro8.Value = poEnGuiaAerea.posicion;

         SqlParameter Parametro9 = cmd.Parameters.Add("@piterminos", SqlDbType.NChar);
         Parametro9.Direction = ParameterDirection.Input;
         Parametro9.Value = poEnGuiaAerea.terminos;

         SqlParameter Parametro10 = cmd.Parameters.Add("@piobservacion", SqlDbType.VarChar);
         Parametro10.Direction = ParameterDirection.Input;
         Parametro10.Value = poEnGuiaAerea.sobservacion;

         int n = cmd.ExecuteNonQuery();
         bExito = (n > 0);

         return bExito;
      }


       public bool Modificar(EnGuiaAerea poEnGuiaAerea, SqlConnection conexion)
      {
         bool bExito = false;
         SqlCommand cmd = new SqlCommand("sExportaciones_GuiaAereaModificar", conexion);
         cmd.CommandType = CommandType.StoredProcedure;

         SqlParameter Parametro1 = cmd.Parameters.Add("@piidGuiaAerea", SqlDbType.Int);
         Parametro1.Direction = ParameterDirection.Input;
         Parametro1.Value = poEnGuiaAerea.idGuiaAerea;

         SqlParameter Parametro2 = cmd.Parameters.Add("@piidFolioDespacho", SqlDbType.Int);
         Parametro2.Direction = ParameterDirection.Input;
         Parametro2.Value = poEnGuiaAerea.idFolioDespacho;

         SqlParameter Parametro3 = cmd.Parameters.Add("@pifechaEmbarque", SqlDbType.VarChar);
         Parametro3.Direction = ParameterDirection.Input;
         Parametro3.Value = poEnGuiaAerea.sfechaEmbarque;

         SqlParameter Parametro4 = cmd.Parameters.Add("@pihoraEmbarque", SqlDbType.VarChar);
         Parametro4.Direction = ParameterDirection.Input;
         Parametro4.Value = poEnGuiaAerea.shoraEmbarque;

         SqlParameter Parametro5 = cmd.Parameters.Add("@piflagEntregado", SqlDbType.VarChar);
         Parametro5.Direction = ParameterDirection.Input;
         Parametro5.Value = poEnGuiaAerea.sflagEntregado;

         SqlParameter Parametro6 = cmd.Parameters.Add("@pifactorAjuste", SqlDbType.Decimal);
         Parametro6.Direction = ParameterDirection.Input;
         Parametro6.Value = poEnGuiaAerea.nfactorAjuste;

         SqlParameter Parametro7 = cmd.Parameters.Add("@piconocimientoEmbarque", SqlDbType.VarChar);
         Parametro7.Direction = ParameterDirection.Input;
         Parametro7.Value = poEnGuiaAerea.sconocimientoEmbarque;

         SqlParameter Parametro8 = cmd.Parameters.Add("@piposicion", SqlDbType.NChar);
         Parametro8.Direction = ParameterDirection.Input;
         Parametro8.Value = poEnGuiaAerea.posicion;

         SqlParameter Parametro9 = cmd.Parameters.Add("@piterminos", SqlDbType.NChar);
         Parametro9.Direction = ParameterDirection.Input;
         Parametro9.Value = poEnGuiaAerea.terminos;

         SqlParameter Parametro10 = cmd.Parameters.Add("@piobservacion", SqlDbType.VarChar);
         Parametro10.Direction = ParameterDirection.Input;
         Parametro10.Value = poEnGuiaAerea.sobservacion;

         int n = cmd.ExecuteNonQuery();
         bExito = (n > 0);

         return bExito;
      }

       public List<EnGuiaAerea> Listar(int pidGuiaAerea, SqlConnection conexion)
      {
         List<EnGuiaAerea> loEnGuiaAerea = null;
         EnGuiaAerea oEnGuiaAerea = null;
         SqlCommand cmd = new SqlCommand("sExportaciones_GuiaAereaListar", conexion);
         cmd.CommandType = CommandType.StoredProcedure;

         SqlParameter Parametro1 = cmd.Parameters.Add("@piidGuiaAerea", SqlDbType.Int);
         Parametro1.Direction = ParameterDirection.Input;
         Parametro1.Value = pidGuiaAerea;

         SqlDataReader drd = cmd.ExecuteReader(CommandBehavior.SingleResult);
         if (drd != null)
         {

            loEnGuiaAerea = new List<EnGuiaAerea>();
            while (drd.Read())
            {
               oEnGuiaAerea = new EnGuiaAerea();
               oEnGuiaAerea.idGuiaAerea = drd.GetInt32(0);
               if (!drd.IsDBNull(1))
               {
                  oEnGuiaAerea.idFolioDespacho = drd.GetInt32(1);
               }
               if (!drd.IsDBNull(2))
               {
                  oEnGuiaAerea.sfechaEmbarque = drd.GetString(2);
               }
               if (!drd.IsDBNull(3))
               {
                  oEnGuiaAerea.shoraEmbarque = drd.GetString(3);
               }
               if (!drd.IsDBNull(4))
               {
                  oEnGuiaAerea.sflagEntregado = drd.GetString(4);
               }
               if (!drd.IsDBNull(5))
               {
                  oEnGuiaAerea.nfactorAjuste = drd.GetDecimal(5);
               }
               if (!drd.IsDBNull(6))
               {
                  oEnGuiaAerea.sconocimientoEmbarque = drd.GetString(6);
               }
               if (!drd.IsDBNull(7))
               {
                  oEnGuiaAerea.posicion = drd.GetString(7);
               }
               if (!drd.IsDBNull(8))
               {
                  oEnGuiaAerea.terminos = drd.GetString(8);
               }
               if (!drd.IsDBNull(9))
               {
                  oEnGuiaAerea.sobservacion = drd.GetString(9);
               }
               loEnGuiaAerea.Add(oEnGuiaAerea);
            }
            drd.Close();
         }

         return loEnGuiaAerea;
      }
   }
}
